/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.hibernate.entities;

import com.oea.hibernate.utilities.HibernateUtil;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author unnati
 */
@Table(name = "tests_questions")
@Entity
public class Test_Question extends AbstractEntity {

    @GeneratedValue

    @Column(name = "id")
    @Id
    protected Long id;

    @Column(name = "tid")
    protected Long tid;

    @Column(name = "qid")
    protected Long qid;
    
    

    @Transient
    protected Question question;
    
    
    
    @Transient
    protected String flag;

    @Transient 
    protected  Test test;

    public Test getTest() {
        return test;
    }

    public void setTest( ) {
        this.test = (Test) new Test().getById(tid);
    }
    
    
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTid() {
        return tid;
    }

    public void setTid(Long tid) {
        this.tid = tid;
    }

    public Long getQid() {
        return qid;
    }

    public void setQid(Long qid) {
        this.qid = qid;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag() {
        this.flag = "1";
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion( ) {
        this.question = (Question) new Question().getById(qid);
    }

    
    public static List<Test_Question> getAllByTestId(long tid) {
        List<Test_Question> li_test_que = null;
        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("from Test_Question where tid=" + tid + "");
            li_test_que = q.list();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return li_test_que;
    }

    public Long getIdByTestAndQuestionId(Long que_id,Long test_id)
    {
        Long l = null;

        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery("select id from Test_Question where qid="+que_id+" and tid="+test_id+"");
            l =(Long)(q.list().get(0));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return l;
    }
    
    public List<Test_Question> getAllByTestAndQuestionId(Long que_id,Long test_id)
    {
        List<Test_Question> li = null;

        try {
            Session s = HibernateUtil.getSessionFactory().openSession();
            Query q = s.createQuery(" from Test_Question where qid="+que_id+" and tid="+test_id+"");
            li =q.list();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return li;
    }

}
